package com.ctsi.system.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.system.domain.Dictionary;
import com.ctsi.system.domain.DictionaryData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 字典类型 Service 类
 *
 * @author zhangjw
 * @description 字典类型
 * @created Fri May 04 15:28:29 CST 2018
 */
@Service
public class DictionaryService {
    private static final Logger logger = LoggerFactory.getLogger(DictionaryService.class);
    @Autowired
    private MybatisGenericDao dao;

    public void setDao(MybatisGenericDao dao) {
        this.dao = dao;
    }

    /*
     * 查询子节点数量
     * */
    public Object selectChildCount(String dictdesc) {
        logger.debug("selectChildCount {}", dictdesc);
        return dao.queryForObject(DictionaryData.class.getName() + ".selectChildCount", dictdesc);
    }

    /**
     * 根据productId获取该产品信息
     */
    public Dictionary selectDescById(String id) {
        logger.debug("selectDescById {}", id);
        return (Dictionary) dao.queryForObject(Dictionary.class.getName() + ".selectDescById", id);
    }

    /**
     * 添加
     */
    public void insertDictionary(Dictionary dictionary) {
        if (UtilValidate.isEmpty(dictionary.getId()))
            dictionary.setId(UUIDGenerator.getId());
        if (dictionary.getAddTime() == null) {
            dictionary.setAddTime(System.currentTimeMillis());
        }
        if (dictionary.getIsdel() == null) {
            dictionary.setIsdel(0);
        }
        dictionary.setIscache(1);
        logger.debug("insert {}", dictionary);
        dao.insert(dictionary);
    }

    /**
     * 动态更新
     */
    public int updateDictionaryByPKSelective(Dictionary dictionary) {
        logger.debug("updateByPrimaryKeySelective {}", dictionary);
        return dao.update(Dictionary.class.getName() + ".updateByPrimaryKeySelective", dictionary);
    }

    /**
     * 更新
     */
    public int updateDictionary(Dictionary dictionary) {
        logger.debug("update {}", dictionary);
        return dao.update(dictionary);
    }


    public Dictionary editDictionaryById(String id) {
        if (UtilValidate.isEmpty(id)) {
            return null;
        }
        logger.debug("editById {}", id);
        return (Dictionary) dao.queryForObject(Dictionary.class.getName() + ".edit", id);
    }

    /**
     * 根据ID查找 Dictionary对象
     */
    public Dictionary findDictionaryById(String id) {
        if (UtilValidate.isEmpty(id)) {
            return null;
        }
        logger.debug("findById {}", id);
        return dao.get(Dictionary.class, id);
    }

    /**
     * 删除Dictionary对象
     */
    public int removeDictionary(String id) {
        if (UtilValidate.isNotEmpty(id)) {
            return dao.delete(Dictionary.class.getName() + ".delete", id);
        }
        //return dao.remove(id);
        return 0;
    }

    /**
     * 详细页查询
     */
    public Map<String, Object> findByDetail(String id) {
        return (Map<String, Object>) dao.queryForObject(Dictionary.class.getName() + ".findByDetail", id);
    }

    /**
     * 多条件查询结果
     */
    public Page<Dictionary> findByMulitCondition(Map<String, Object> param, int start, int pageSize) {
        logger.debug("findByMulitCondition {}", param);
        if (pageSize < 0 || pageSize > 2000) {
            pageSize = Page.DEFAULT_PAGE_SIZE;
        }
        if (start != -1) {
            return dao.queryPage(Dictionary.class.getName() + ".selectMulitCondition", param, start, pageSize);
        } else {
            throw new RuntimeException("参数不正确 !!");
        }
    }

    /**
     * 多条件查询结果不分页
     */
    public List<?> find(Map<String, Object> map) {
        logger.debug("find {}", map);
        return dao.queryForList(Dictionary.class.getName() + ".selectByMap", map);
    }

}
